export default {
  
  fileInfo(path) {
    // 路径中的文件信息
    return {
      name: path.replace(/(.*\/)*([^.]+).*/gi, "$2"),
      type: path.replace(/.+\./, "").toLowerCase(),
      folder: path.replace(/\.\w+$/, ""),
      fullName: path.split("/").pop()
    };
  },
  fileToDataURL(file, callback) {
    // 文件 转 base64
    let freader = new FileReader();
    freader.readAsDataURL(file);
    freader.onload = function(e) {
      callback(e.target.result);
    };
  },
  blobToDataURL(blob, callback) {
    // blob 转 base64
    let freader = new FileReader();
    freader.readAsDataURL(blob);
    freader.onload = function(e) {
      callback(e.target.result);
    };
  },
  dataURLtoBlob(dataurl) {
    // base64 转 blob
    let arr = dataurl.split(","),
      mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]),
      n = bstr.length,
      u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
  },
  dataURLtoFile(dataurl, filename) {
    // base64 转 file ,IE 不可用
    let arr = dataurl.split(","),
      mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]),
      n = bstr.length,
      u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
  }
};
